package com.goldze.mvvmhabit.ui.news;

import android.arch.lifecycle.Observer;
import android.arch.lifecycle.ViewModelProviders;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;

import com.goldze.mvvmhabit.R;
import com.goldze.mvvmhabit.app.AppViewModelFactory;
import com.goldze.mvvmhabit.databinding.ActivityNewsDetailBinding;
import com.goldze.mvvmhabit.utils.GlideUtil;

import me.goldze.mvvmhabit.base.BaseActivity;
import me.goldze.mvvmhabit.utils.LogUtil;

public class NewsDetailActivity extends BaseActivity<ActivityNewsDetailBinding, NewsDetailViewModel> {


    //ActivityLoginBinding类是databinding框架自定生成的,对应activity_login.xml
    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.activity_news_detail;
    }

    @Override
    public int initVariableId() {
        return com.goldze.mvvmhabit.BR.viewModel;
    }

    @Override
    public NewsDetailViewModel initViewModel() {
        //使用自定义的ViewModelFactory来创建ViewModel，如果不重写该方法，则默认会调用LoginViewModel(@NonNull Application application)构造方法
        AppViewModelFactory factory = AppViewModelFactory.getInstance(getApplication());
        return ViewModelProviders.of(this, factory).get(NewsDetailViewModel.class);
    }

    @Override
    public void initViewObservable() {
        String id = getIntent().getStringExtra("module_id");
        LogUtil.e("新闻详情id-->"+ id);
        if (!TextUtils.isEmpty(id)) {
            viewModel.getNewsDetail(id);

        }

        //图片赋值
        viewModel.url.observe(this, new Observer<String>() {
            @Override
            public void onChanged(@Nullable String s) {
                GlideUtil.newCacheInstance(s,binding.ivModule);
            }
        });
    }

}
